const SERVICE = require("../../lib/js/service.js");
const Golbal = require("../../lib/js/global.js");

Page({
  data:{
    imgUrl: SERVICE.imgUrl,
    sex: 0,   //性别
    site: null,   //地址
    user_avatr: null,
    certification: ["未认证", "个体认证", "企业认证", "4S店认证", "审核中 ...", "审核不通过"]
  },
  
  onShow(){
    this.setData({
      userInfo: wx.getStorageSync("userInfo"),  //用户信息      
      sex: wx.getStorageSync("userInfo").sex
    });
  },

  /*分享*/ 
  onShareAppMessage(){},
  
  onUnload(){
    
  },

  /*上传图片*/
  uploadImg(){
    let _this = this, td = this.data;
    wx.chooseImage({
      success: function(imgRes) {
        wx.showLoading({
          title: "上传头像中...",
          mask: true
        });
        wx.uploadFile({
          url:  'https://www.csdashi.cn/' + SERVICE.URL.saveEditHeadImg,
          filePath: imgRes.tempFiles[0].path,
          name: "image",
          formData:{
            thirdCookie: wx.getStorageSync("thirdCookie")
          },
          success: function(res){
            wx.hideLoading();
            let res_data = JSON.parse(res.data);
            if (res_data.status != "success"){
              wx.showToast({
                title: "上传失败",
                icon: "none",
                mask: true
              });
            }else{
              _this.setData({
                user_avatr: imgRes.tempFiles[0].path
              });
            }
          },
          fail(){
            wx.showToast({
              title: "上传失败",
              icon: "none",
              mask: true
            });
          }
        })
      },
    })
  },
  
  /*获取认证*/ 
  getCertification(e){
    let certification = e.currentTarget.dataset.certification;
    if (certification == 0){
      wx.navigateTo({
        url: "../auth-info/auth-info",
      })
    }
  },

  /*性别*/
  getSex(e){
    let td = this.data;
    this.setData({
      sex: e.currentTarget.dataset.sex
    });
    this.editPersonalData({});
  },

  /*编辑公司名字*/
  compileCompanyName(e){
    if (e.detail.value.length > 0){
      this.editPersonalData({
        corpName: e.detail.value
      });
    }
  },
  
  /*名字*/
  compileName(e){
    if (e.detail.value.length > 0){
      this.editPersonalData({
        userName: e.detail.value
      });
    }
  },

  /*电话*/
  compileMobile(e){
    let mobile = e.detail.value;
    if (mobile.length > 0 && Golbal.checkPhone(mobile) == false){
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true,
        duration: 1500
      });
    }else{
      this.editPersonalData({
        phone: e.detail.value
      });
    }
   
  },
  
  /*获取地址*/
  getSite(e){
    let site = e.detail.value;
    console.log();
    this.setData({
      site: site[0] + '/' + site[1] + '/' + site[2]
    });
    this.editPersonalData({
      corpAddress: site[0] + '/' + site[1] + '/' + site[2]
    });
  },
   
  /*详细地址*/
  getDetailedAddress(e){
    let detailed_address = e.detail.value;
    if(detailed_address.length > 0){
      this.editPersonalData({
        detailAddress: e.detail.value
      });
    }
  },

  /*编辑个人资料*/
  editPersonalData(params){
    wx.showLoading({
      title: "编辑中...",
      mask: true
    });

    let td = this.data;
    params.sex = td.sex;
    params.thirdCookie = wx.getStorageSync("thirdCookie");
    SERVICE.POST(SERVICE.URL.editPersonalData, params, (status, res) => {
      wx.hideLoading();
      if (res.data.status == "success") {
        wx.showToast({
          title: "编辑成功",
          icon: "none",
          mask: true,
          duration: 1500
        });
      }else{
        wx.showToast({
          title: "编辑失败",
          icon: "none",
          mask: true,
          duration: 1500
        });
      }
    });
  },
  logOut(){
    wx.showModal({
      title: "提示",
      content: "确定注销吗",
      success: function(res){
        if(res.confirm){
          wx.clearStorageSync();
          wx.reLaunch({
            url: "../login/login",
          });
        }
      }
    })
  }
});